<?php 
/**
 * 黑市模块
 */
class Module_blackmarket{
	function __construct(){
	
	}
	/**
	 * 黑市页面初始
	 */
	function index(){
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('Currency');
		/*********读取筹码********/
		$chip = Currency::get('app_21', $uin , 'currency');
		/*********读取银币********/
		$silver_coin = Currency::get('app_factory', $uin , 'currency');
		$smarty->assign('chip',$chip);
		$smarty->assign('silver_coin',$silver_coin);
		return $smarty->fetch('app/blackmarket/index.tpl.php');
		
	}
	/**
	 * 进入兑换货币页面
	 */
	function convert(){
		$Changing = isset($_POST['Changing']) ? trim($_POST['Changing']) : null;	
		$silver_coin = isset($_POST['silver_coin']) ? intval($_POST['silver_coin']) : null;
		$chip = isset($_POST['chip']) ? intval($_POST['chip']) : null;
		
		if ($Changing == 'silver_coin'){
			return View::error('暂不支持银币兑换');
		}
		$smarty = loadSmarty();
		$smarty->assign('Changing',$Changing);
		$smarty->assign('silver_coin',$silver_coin);
		$smarty->assign('chip',$chip);
		return $smarty->fetch('app/blackmarket/convert.tpl.php');
		
	}
	/**
	 * 开始兑换货币
	 */
	function currency(){
		$Changing = isset($_POST['Changing']) ? trim($_POST['Changing']) : null;	
		$currency = isset($_POST['currency']) ? intval($_POST['currency']) : null;
		$uin = sessionInfo('uin');
		loadLib('Currency');
		
		/*************收取5%手续费****************/
		$stake = intval($currency * 0.95);
		if($currency < 10 || $currency % 10 != 0) {			
				return View::error('你输入的筹码或银币不符合规范,请输入筹码或银币为10的倍数!');
		}
		/*****************换取筹码******************/
		if($Changing == 'chip'){
			/*********读取银币********/
			$silver_coin = Currency::get('app_factory', $uin , 'currency');
			if($silver_coin < $currency) {			
				return View::error('你的银币不足' . $currency . ',不能兑换');
			}
			/*********加筹码**********/
			$code = Currency::update('app_21' , $uin , +$stake);
			
			if($code == 0){
				/*********扣银币**********/
				Currency::update('app_factory' , $uin , -$currency);
				
				/*********记录用户操作**********/
				loadLib('Operate');
				Operate::getAdd($uin , 0 , 'chip');
				
				//防止刷新
				header('Location: ' . SITEURL . '/app.php?m=blackmarket&sid=' . sessionInfo('skey') );
			}
		}
		/****************换取银币*******************/
		if($Changing == 'silver_coin'){
			/*********读取筹码********/
			$chip = Currency::get('app_21', $uin , 'currency');
			if($chip < $currency) {			
				return View::error('你的筹码不足' . $currency . ',不能兑换');
			}
			/*********加银币**********/
			$code = Currency::update('app_factory' , $uin , +$stake);
			if($code == 0){
				/*********扣筹码**********/
				Currency::update('app_21' , $uin , -$currency);
				
				/*********记录用户操作**********/
				loadLib('Operate');
				Operate::getAdd($uin , 0 , 'silver_coin');
				//防止刷新
				header('Location: ' . SITEURL . '/app.php?m=blackmarket&sid=' . sessionInfo('skey') );
			}
		}
	}
	/**
	 * 三国美女兑换筹码
	 */
	function belle_chip(){
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		loadLib('Currency');
		/****************************查询个人的筹码***********************/
		$chip = Currency::get('app_21', $uin , 'currency');
		
		loadLib('App_kingdomslady');
		/************************查询拥有的美女********************/
		$list = App_kingdomslady::getAllPossess($uin, 'belle');
		/************************获取时间********************/
		$time = App_kingdomslady::getTime();
		/************************获取一个时间段的内容********************/
		$coun_time = App_kingdomslady::getCountTime($uin , 'belle_convert' , 0 , $time[0]);
		
		if (!empty($coun_time)){
			App_kingdomslady::getAlterCount($uin, 'belle_convert' ,5);
		}
		if(!empty($list)){
			foreach ($list as $k => $row){
				$is_list[] = $row['object_id'];
				
			}
		}
		$count = App_kingdomslady::getOneCount($uin , 'belle_convert' , false ,5);
		$newbie = false;
		if(empty($count)) {
			$count = App_kingdomslady::getOneCount($uin , 'belle_convert' ,true ,5);
			$newbie = true;
		}
		/************************查询美女表***********************/
		$belle = App_kingdomslady::getAllBelle();
		if(!empty($list)){
			foreach ($list as $k => $row){
				foreach ($belle as $j => $result){
					if($row['object_id'] == $result['id']){
						$list[$k]['belle'] = $result;
					}
				}
			}
			$smarty->assign('list',$list);
		}
		//print_r($count);
		$smarty->assign('count',$count);
		$smarty->assign('chip',$chip);
		return $smarty->fetch('app/blackmarket/belle.tpl.php');
	}
	/**
	 * 收取5%手续费
	 */
	function is_belle_convert(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$uin = sessionInfo('uin');
		loadLib('App_kingdomslady');
		/**********************查询三国美女**********************/
		$list = App_kingdomslady::getOnePossess($uin , $id);
		if (empty($list)){
			return View::error('你没有该美女!!!');
		}
		/***********************查询美女资料**************************/
		$belle = App_kingdomslady::getOneBelle($list['object_id']);
		if (empty($belle)){
			return View::error('没有该美女资料!!!');
		}
		$stake = $belle['chip'] * (1 - 0.05);
		$is_count = App_kingdomslady::getOneCount($uin , 'belle_convert');
		
		if ($is_count['count'] <= 0){
			return View::error('今天的兑换已达到上限,明天再兑换吧!!!');
		}
		$is_count = $is_count['count'] - 1;
		loadLib('Currency');
		/*************************加筹码**************************/
		$code = Currency::update('app_21' , $uin , +$stake);
		if ($code == 0){
		
			/********************************获取数量**************************/
			$count = App_kingdomslady::getOneCount($uin, 'belle');
			$count = $count['count'] - 1;
			/**********************修改美女总数***********************/
			App_kingdomslady::getAlterCount($uin, 'belle', $count);
			/**********************修改交易总数***********************/
			App_kingdomslady::getAlterCount($uin, 'belle_convert', $is_count);
			App_kingdomslady::delPossess($uin, $id);
			header('Location: ' . SITEURL . '/app.php?m=blackmarket&a=belle_chip&id='. $id .'&sid=' . sessionInfo('skey') );
		}else{
			return View::error('网络繁忙,请稍后再试！');
		}
	}
	/**
	 * 查看消费记录
	 */
	function consume_money(){
		$uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		// 如果没有uin参数，则显示自己的个人主页
		if(empty($uin)) {
			$uin = $GLOBALS['CORE']['class']['Session']->getUin();
		}
		// 获取用户数据,用于判断uin用户内容是否存在
		loadLib('User');
		$userinfo = USER::getFullInfo($uin);
		if(empty($userinfo)){
			return View::error('该用户信息不存在' );
		}
		// 准备个人主页导航数据
		View::prepare_profile_nav_data($uin);
		
		//查询消费记录
		loadLib('Operate');
		//购买的道具查询
		loadLib('App_prop'); 
		$list = Operate::getAllConsume($uin);
		$count = count($list);
		if (!empty($list)){
			foreach ($list as $k => $row){
				if ($row['type'] == 'app_kingdoms_prop'){
					$list[$k]['type'] = '三国夫人';
					$arr_list = App_prop::getOneProp('app_kingdoms_prop', $row['to_id']);
					$list[$k]['name'] = $arr_list['name'];
				}
				if ($row['type'] == 'app_21_prop'){
					$list[$k]['type'] = '21点';
					$arr_list = App_prop::getOneProp('app_21_prop', $row['to_id']);
					$list[$k]['name'] = $arr_list['name'];
				}
				if ($row['type'] == 'app_mora_prop'){
					$list[$k]['type'] = '猜拳';
					$arr_list = App_prop::getOneProp('app_mora_prop', $row['to_id']);
					$list[$k]['name'] = $arr_list['name'];
				}
				if ($row['type'] == 'app_showhand_prop'){
					$list[$k]['type'] = '梭哈';
					$arr_list = App_prop::getOneProp('app_showhand_prop', $row['to_id']);
					$list[$k]['name'] = $arr_list['name'];
				}
			}
		}
		$smarty = loadSmarty();
		
		/*******************数据分页******************/
		$page = ($page-1) * $per_page;
		$is_list_array = array_slice($list,$page,$per_page);
		
		$smarty->assign('list',$is_list_array);
		$smarty->assign('per_page',$per_page);
		$smarty->assign('count',$count);
		return $smarty->fetch('app/blackmarket/consume.tpl.php');
	}
}
# enf of script